$(function () {
    //退出
    var layer = layui.layer;
    $('#tuichu').on('click', function () {
        layer.confirm('确认退出？', { icon: 3, title: '提示' }, function (index) {
            localStorage.removeItem('token')
            location.href = '/login.html'
            layer.close(index);
        });
    })

    // 获取用户的基本信息
    getUserInfo()
    function getUserInfo() {
        $.ajax({
            type: 'get',
            url: '/my/userinfo',
            success: function (res) {
                if (res.code !== 0) {
                    return layer.msg('获取用户信息失败!!!')
                }
                renderAvatar(res.data)
            },

        })
    }

    function renderAvatar(data) {
        var name = data.nickname || data.username
        var first = name.charAt(0).toUpperCase();
        //没有头像
        if (data.user_pic == null) {
            $('.user-info-box').html(`<div class="text-avatar">${first}</div><span class="welcome">&nbsp;欢迎您&nbsp;${name}</span>`)
            $('#header-avatar').html(`<div class="text-avatar">${first}</div>个人中心`)
        } else {
            $('#header-avatar').html(`<img src="${data.user_pic}" class="layui-nav-img">个人中心`)
            // 侧边栏区域
            $('.user-info-box').html(`<img src="${data.user_pic}" class="layui-nav-img">
            <span class="welcome">&nbsp;欢迎您&nbsp; ${name}</span>`)
        }
    }
})
